package com.adkfp.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class UserRight {
    private Connection conn ;
    
    private String dbdriver ;
    private String dbhost;
    private String dbuser;
    private String dbpassword;

    public UserRight() throws IOException, ClassNotFoundException, SQLException  {
    	Properties properties = new Properties();
    	// 使用ClassLoader加载properties配置文件生成对应的输入流
    	//InputStream in = Reader.class.getResourceAsStream("/database.properties"); 
    	InputStream in = Reader.class.getResourceAsStream("/database.properties");
    	// 使用properties对象加载输入流
        properties.load(in);
    	//获取key对应的value值
		
    	Class.forName(properties.getProperty("jdbc.driverClass"));//"com.mysql.jdbc.Driver");
		dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
		dbuser = properties.getProperty("jdbc.user","root");
		dbpassword = properties.getProperty("jdbc.password", "123456");
		conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
    }
    
	public UserRight(String dbdriver, String dbhost, String dbuser, String dbpassword) throws ClassNotFoundException, SQLException {
		super();
		this.dbdriver = dbdriver;
		this.dbhost = dbhost;
		this.dbuser = dbuser;
		this.dbpassword = dbpassword;
    	Class.forName(this.dbdriver);//"com.mysql.jdbc.Driver");
    	conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
	}
	
	public void ConnectDB() throws SQLException{
    	conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
    }
	
	protected void finalize() throws SQLException{
    	conn.close();
    }
	
	public Boolean CheckRight(Integer UserID,String url) throws ClassNotFoundException, SQLException, IOException {
		Boolean RetVal = false ;
 		Statement state = conn.createStatement();
		String SQLStr = " SELECT c.* from adk_manage_user_role a" + 
				        "  INNER JOIN adk_manage_role_page b on a.role_id = b.role_id" + 
				        "  INNER JOIN adk_manage_page c on b.page_id = c.id" + 
				        " where a.user_id = " + UserID;
		ResultSet rs = state.executeQuery(SQLStr);
        while(rs.next()){
        	if(rs.getString("url") .equals(url)) {
        		RetVal = true ;
        		break;
        	}
        	
        }
        state.close();
        System.out.println("RetVal"+RetVal);
        return RetVal ;
    }
}
